<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style>
        
    </style>
</head>
<body>
    
</body>
<script>
    // 编写一个函数，输入n为偶数时，调用函数求1/2+1/4+...+1/n,当输入n为奇数时，调用函数求1+1/3+...+1/n 

    // 1/2+1/4+...+1/n

    // fn(6) = 1/6 + fn(4)
                //fn(4) = 1/4 + 1/2
                            //  fn(2) = 1/2
    // fn(7) = 1/7 + 1/5 + 1/3 + 1/1
            //   fn(5) = 1/5 + 1/3 + 1/1
                        //     fn(3) = 1/3 + 1/1
                                        //   fn(1) = 1
    // fn(n) = 1/n + fn(n-2)
    // fn(2) = 1/2
    // fn(1) = 1

    function fn(n){
        if(n === 1){
            return 1;
        }
        if(n === 2){
            return 1/2;
        }
        return 1/n + fn(n-2);
    }
    console.log(fn(6));

    function fun(n){
        var sum = 0;
        if( n%2 ){
            for(var i=1;i<=n;i+=2){
                sum += 1/i;
            }
        }else{
            for(var i=2;i<=n;i+=2){
                sum += 1/i;
            }
        }
        return sum;
    }
    console.log(fun(6));

    

</script>
</html>